import { Button, DatePicker, Input, NavBar } from 'antd-mobile'
import Icon from '@/page/Month/components/Icon'
import './index.scss'
import classNames from 'classnames'
import { billListData } from '@/contants'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
import dayjs from 'dayjs'
import { addBillData } from '@/store/modules/billStore'
import { useDispatch } from 'react-redux'
const New = () => {
  const navigate = useNavigate()
   
  const [checked,setChecked] = useState('pay');
  const [visibleShow,setvisibleShow] = useState(false)
  const [timeVal, setTimeVal] = useState("")
  const [type, setType] = useState('')
  const [moneyValue,setMoneyValue] = useState(0.00)
  
  const dispatch = useDispatch();
  const save = () => {
     console.log(checked,timeVal,type,moneyValue)  
     const data = {
        type : checked,
        useFor: type,
        money: checked === 'pay' ?  -moneyValue : +moneyValue,
        date: timeVal ? dayjs(timeVal).format("YYYY-MM-DD") : new Date()
     }
      dispatch(addBillData(data))
      navigate('/')
  }
  return (
    <div className="keepAccounts">
      <NavBar className="nav" onBack={() => navigate(-1)}>
        记一笔
      </NavBar>

      <div className="header">
        <div className="kaType">
          <Button
            shape="rounded"
            className={classNames(checked === 'pay' && 'selected')}
            onClick={() => setChecked('pay')}
          >
            支出
          </Button>
          <Button
            className={classNames(checked === 'income' && 'selected')}
            shape="rounded"
            onClick={() => setChecked('income')}
          >
            收入
          </Button>
        </div>

        <div className="kaFormWrapper">
          <div className="kaForm">
            <div className="date">
              <Icon type="calendar" className="icon" />
              <span className="text" onClick={() => setvisibleShow(true)}>{!timeVal ? '今天' : dayjs(timeVal).format("MM-DD")}</span>
              <DatePicker
                className="kaDate"
                title="记账日期"
                visible={visibleShow}
                onClose={() => {
                  setvisibleShow(false)
                }}
                onConfirm={val => {
                  setTimeVal(val)
                }}
                max={new Date()}
              />
            </div>
            <div className="kaInput">
              <Input
                className="input"
                placeholder="0.00"
                type="number"
                value={moneyValue}
                onChange={val => {
                   setMoneyValue(val)
               }}
              />
              <span className="iconYuan">¥</span>
            </div>
          </div>
        </div>
      </div>

      <div className="kaTypeList">
        {billListData[checked].map(item => {
          return (
            <div className="kaType" key={item.type}>
              <div className="title">{item.name}</div>
              <div className="list">
                {item.list.map(item => {
                  return (
                    <div
                      className={classNames(
                        'item',
                        type === item.type && 'selected'
                      )}
                      key={item.type}
                      onClick={()=> setType(item.type)}
                    >
                      <div className="icon">
                        <Icon type={item.type} />
                      </div>
                      <div className="text">{item.name}</div>
                    </div>
                  )
                })}
              </div>
            </div>
          )
        })}
      </div>

      <div className="btns">
        <Button className="btn save" onClick={save}>
          保 存
        </Button>
      </div>
    </div>
  )
}

export default New